{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d8e9ac16",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 第二周第二节\n",
    "## 数组算术\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09ca4b1b",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "1. 基本运算，加减乘除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "da8a6a42",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "#定义一个numpy数组\n",
    "import numpy as np\n",
    "arr1 = [[1,2,3],\n",
    "        [4,5,6]]\n",
    "arr1 = np.array(arr1)\n",
    "arr2 = [[4,5,6],\n",
    "        [7,8,9]]\n",
    "arr2 = np.array(arr2)\n",
    "print(arr1)\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "1240819b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4 10 18]\n",
      " [28 40 54]]\n"
     ]
    }
   ],
   "source": [
    "print(arr1*arr2) #数组相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "86d41db7",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5  7  9]\n",
      " [11 13 15]]\n"
     ]
    }
   ],
   "source": [
    "print(arr1+arr2) #数组相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "68f8a791",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-3 -3 -3]\n",
      " [-3 -3 -3]]\n"
     ]
    }
   ],
   "source": [
    "print(arr1-arr2) #数组相减"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "370bf97a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.25       0.4        0.5       ]\n",
      " [0.57142857 0.625      0.66666667]]\n"
     ]
    }
   ],
   "source": [
    "print(arr1/arr2) #数组相除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "38706152",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  4  9]\n",
      " [16 25 36]]\n"
     ]
    }
   ],
   "source": [
    "print(arr1**2) #数组的乘方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "6be522cf",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False]\n",
      " [False False False]]\n",
      "[[ True  True  True]\n",
      " [ True  True  True]]\n"
     ]
    }
   ],
   "source": [
    "#数组的比较\n",
    "print(arr1>arr2)\n",
    "print(arr1<arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d2809ac",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "2. 数组的索引与切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "c93c7b05",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "[3 4]\n",
      "[1 2 5 6 5 6 7 8]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1,2,3,4,5,6,7,8])\n",
    "print(arr[2]) #获取下标为2的数组内值\n",
    "print(arr[2:4]) #获取一个切片数组，左闭右开的区间[）, 后面的索引取不到\n",
    "arr[2] = 5 #数组内部值的替换\n",
    "arr[2:4] = [5,6] #多个连续值的替换\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "fe159358",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(arr1[0][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "3997b565",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 5 6]\n"
     ]
    }
   ],
   "source": [
    "#从开始到某个下标进行索引\n",
    "print(arr[:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f256ab9",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 二维数组/矩阵的切片\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "e2cbea01",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "data = np.array([[1,2,3],\n",
    "                 [4,5,6],\n",
    "                 [7,8,9]])\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "474e36fe",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 3]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "source": [
    "#取数组的右上角\n",
    "print(data[:2,1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "93d8abf1",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 5]]\n"
     ]
    }
   ],
   "source": [
    "#取数组第二行前两个\n",
    "print(data[1:2,:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abaac230",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 数组的转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "0a0af125",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4 7]\n",
      " [2 5 8]\n",
      " [3 6 9]]\n"
     ]
    }
   ],
   "source": [
    "print(data.T)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d32495f",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 数组的通用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "10831028",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "data =np.array([[1,2,3,4,5,6],[1,2,3,4,5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "079ccdca",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4 5 6]\n",
      " [1 2 3 4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "# np.abs 计算整数，浮点数以及复数的绝对值\n",
    "print(np.abs(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "199ea62c",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.         1.41421356 1.73205081 2.         2.23606798 2.44948974]\n",
      " [1.         1.41421356 1.73205081 2.         2.23606798 2.44948974]]\n"
     ]
    }
   ],
   "source": [
    "#np.sqrt 计算每个元素的平方根\n",
    "print(np.sqrt(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "678c53c0",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  4  9 16 25 36]\n",
      " [ 1  4  9 16 25 36]]\n"
     ]
    }
   ],
   "source": [
    "# np.square 计算平方\n",
    "print(np.square(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "a215374c",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  2.71828183   7.3890561   20.08553692  54.59815003 148.4131591\n",
      "  403.42879349]\n",
      " [  2.71828183   7.3890561   20.08553692  54.59815003 148.4131591\n",
      "  403.42879349]]\n"
     ]
    }
   ],
   "source": [
    "# np.exp 计算每个值的指数值e^x\n",
    "print(np.exp(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "35f896b0",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.         0.69314718 1.09861229 1.38629436 1.60943791 1.79175947]\n",
      " [0.         0.69314718 1.09861229 1.38629436 1.60943791 1.79175947]]\n"
     ]
    }
   ],
   "source": [
    "#np.log ,log10,log2,log1p 计算自然对数值\n",
    "print(np.log(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "62119930",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 2. 3. 4. 5. 6.]\n",
      " [1. 2. 3. 4. 5. 6.]]\n"
     ]
    }
   ],
   "source": [
    "# np.ceil, np.floor 分别是取某小数的最高整数值1.1 = 2,1.5 = 2，和最低整数值1.2 = 1 , 1.6 = 1\n",
    "print(np.ceil(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "90c94a0c",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False False False False]\n",
      " [False False False False False False]]\n"
     ]
    }
   ],
   "source": [
    "# np.isnan 判断数组中元素是否为空值 NAN\n",
    "print(np.isnan(data))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79ca2843",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 数学统计方法\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58781a36",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "1. sum 求和\n",
    "2. mean 平均数\n",
    "3. min,max 最小值，最大值\n",
    "4. std, var 标准差，方差\n",
    "5. argmin, argmax 最小值的位置，最大值的位置\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "31fb4d30",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function ndarray.sum>"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(np.sum(data)) #和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "605ed329",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n"
     ]
    }
   ],
   "source": [
    "print(np.mean(data)) #平均数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "8c2a9550",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 上面列举的其他方法也相同方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6700e27",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}